package com.cheng.mybatis02.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

public class SqlSessionUtils {

    private static final SqlSessionFactory sqlSessionFactory;

    private SqlSessionUtils(){}

    static {
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getSqlSession(boolean flag){
        return sqlSessionFactory.openSession(flag);
    }

    public static SqlSession getSqlSession(){
        return getSqlSession(true);
    }
}
